library(tidyverse)
library(patchwork)
library(fs)
library(haven)
library(wacolors)
library(sf)
library(ggtext)
library(glue)
library(kableExtra)


cd_shp <- read_rds("data/shp_cd.rds") # from donnermap
st_shp <- read_rds("data/shp_st.rds")

runs <- c("full", "nocontradict")
for (x in runs) {
  if (x == "full") {
    file1 <- "data/mrp-ests_by-cd-race_replicate.csv"
    file4 <- "tables/coalition_counts_2020_replicate.tex"
  } else if (x == "nocontradict") {
    file1 <- "data/mrp-ests_by-cd-race_noc.csv"
    file4 <- "tables/coalition_counts_2020_noc.tex"
  }
# Data -----
cc_cd <- read_csv(path(file1))


# Coalition computation -----
compute_coal <- function(tbl) {
    tbl |>
    summarize(
        black_alone_D = sum((race == "Black")*(1 - p_mrp_twway)*N) / sum(N),
        hisp_alone_D  = sum((race == "Hispanic")*(p_mrp_twway < 0.5)*(1 - p_mrp_twway)*N) / sum(N),
        white_alone_D = sum((race == "White")*(1 - p_mrp_twway)*N) / sum(N),
        other_alone_D = sum((race == "Other")*(1 - p_mrp_twway)*N) / sum(N),
        black_hisp_D  = sum((race %in% c("Hispanic", "Black"))*(p_mrp_twway < 0.5)*(1 - p_mrp_twway)*N) / sum(N),
        bho_D  = sum((!race %in% c("White"))*(p_mrp_twway < 0.5)*(1 - p_mrp_twway)*N) / sum(N),
        white_other_D = sum((race %in% c("White", "Other"))*(1 - p_mrp_twway)*N) / sum(N),
        white_alone_R = sum((race == "White")*(p_mrp_twway)*N) / sum(N),
        hisp_alone_R = sum((race == "Hispanic")*(p_mrp_twway)*N) / sum(N),
        black_hisp_R = sum((race %in% c("Hispanic", "Black"))*(p_mrp_twway)*N) / sum(N),
        bho_R = sum((!race %in% "White")*(p_mrp_twway)*N) / sum(N),
        .groups = "drop"
    ) |>
        mutate(
            coalition = case_when(
                white_alone_R > 0.5 ~ "White alone Republican",
                hisp_alone_R > 0.5 ~ "Hispanic alone Republican",
                black_hisp_R > 0.5 ~ "Black and Hispanic Republican",
                bho_R > 0.5 ~ "Black + Hispanic + Other Republican",
                white_alone_D > 0.5 ~ "White alone Democrat",
                black_alone_D > 0.5 ~ "Black alone Democrat",
                hisp_alone_D > 0.5  ~ "Hispanic alone Democrat",
                black_hisp_D > 0.5  ~ "Black + Hispanic Democrat",
                bho_D > 0.5  ~ "Black + Hispanic + Other Democrat",
                TRUE ~ NA_character_
            )
        )
}


cd_coal <- cc_cd |>
    group_by(year, cd, pct_trump) |>
    compute_coal()

# xtabs ----
lbl_f <- function(x) str_remove_all(x, "\\s(Democrat|Republican)")
cd_20 <- cd_coal |>
    mutate(winner = ifelse(pct_trump > 0.5, "R", "D"),
           coalition = lbl_f(coalition)) |>
    filter(year == 2020) |>
    count(winner, coalition)

xtabs_coal <- cd_20 |>
    pivot_wider(id_cols = coalition,
                names_from = c(winner),
                values_from = n, values_fill = 0) |>
    rename(type = coalition) |>
    mutate(type = recode_factor(
        type,
        "White alone" = "White voters alone",
        "Black alone" = "Black voters alone",
        "Hispanic alone" = "Hispanic voters alone",
        "Black + Hispanic" = "Black and Hispanic voters",
        "Black + Hispanic + Other" = "Black, Hispanic, and Other non-White voters",
        .missing = "White and non-White"
    )) |>
    arrange(type) |>
    add_row(type = "Total", D = sum(cd_20$n[cd_20$winner == "D"]), R = sum(cd_20$n[cd_20$winner == "R"])) |>
    kbl(booktabs = TRUE,
        align = c("r", rep("c", 4)),
        format = "latex",
        linesep = "",
        col.names = c("Racial Group Composition for Sufficient for a Majority", "Democratic CDs", "Republican CDs")) |>
        row_spec(6, hline_after = TRUE)

if (x == "full") {
  full_20 <- cd_20 |>
  pivot_wider(id_cols = coalition,
              names_from = c(winner),
              values_from = n, values_fill = 0) |>
    rename(type = coalition) |>
    mutate(type = recode_factor(
      type,
      "White alone" = "White voters alone",
      "Black alone" = "Black voters alone",
      "Hispanic alone" = "Hispanic voters alone",
      "Black + Hispanic" = "Black and Hispanic voters",
      "Black + Hispanic + Other" = "Black, Hispanic, and Other non-White voters",
      .missing = "White and non-White"
    )) |>
    arrange(type) |>
    add_row(type = "Total", D = sum(cd_20$n[cd_20$winner == "D"]), R = sum(cd_20$n[cd_20$winner == "R"]))
} else if (x == "nocontradict") {
  noc_20 <- cd_20 |>
    pivot_wider(id_cols = coalition,
                names_from = c(winner),
                values_from = n, values_fill = 0) |>
    rename(type = coalition) |>
    mutate(type = recode_factor(
      type,
      "White alone" = "White voters alone",
      "Black alone" = "Black voters alone",
      "Hispanic alone" = "Hispanic voters alone",
      "Black + Hispanic" = "Black and Hispanic voters",
      "Black + Hispanic + Other" = "Black, Hispanic, and Other non-White voters",
      .missing = "White and non-White"
    )) |>
    arrange(type) |>
    add_row(type = "Total", D = sum(cd_20$n[cd_20$winner == "D"]), R = sum(cd_20$n[cd_20$winner == "R"])) |>
    subset(select = -c(type))
}
  

xtabs_coal |>
    write_lines(file4)

}

combined_2020 <- full_20 |> cbind(noc_20) |>
  kbl(booktabs = TRUE,
      align = c("r", rep("c", 4)),
      format = "latex",
      linesep = "",
      col.names = c("Racial Group Composition for Sufficient for a Majority", "Democratic CDs", "Republican CDs", "Democratic CDs", "Republican CDs")) |>
  add_header_above(c(" " = 1, "Original Results" = 2, "Contradictors Dropped" = 2)) |>
  row_spec(6, hline_after = TRUE)

combined_2020 |>
  write_lines("tables/coalition_2020_combined.tex")
  
